package com.jike.jkgame;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.viewpagerindicator.CirclePageIndicator;

public class TreasureCollectionActivity extends Activity implements OnClickListener{

	private PagerAdapter mAdapter;
	private ViewPager mPager;
	private CirclePageIndicator mIndicator;
	private List<GridView> mPages = new ArrayList<GridView>();
	private LayoutInflater mInflater;
	
	private OnItemClickListener mItemClickListner = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.treasures_collection_layout);
		mInflater = LayoutInflater.from(this);
		mAdapter = new ItemPagerAdapter();
		
		findViewById(R.id.iv_back).setOnClickListener(this);
		
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);

		mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		mIndicator.setViewPager(mPager);
		
		initPages();
	}
	
	private void initPages() {
		mPages.clear();
		for (int i = 0; i < 5; ++i) {
			GridView view = (GridView) mInflater.inflate(
					R.layout.items_pager_layout, null, false);
			view.setAdapter(new ItemsAdapter());
			view.setOnItemClickListener(mItemClickListner);
			mPages.add(view);
		}
	}
	
	class ItemPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mPages.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((GridView) object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager) container).addView(mPages.get(position));

			return mPages.get(position);
		}

	}
	
	class ItemsAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return 16;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;

			if (view == null) {
				view = mInflater.inflate(R.layout.strength_item_layout, parent,
						false);
			}

			return view;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		default:
			break;
		}
		
	}
	
}
